import ajax from '../network/index'

// pages/project/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        packs: [],
        auditingBtn: false,
        editPackBtn: false,
        planResult: [],
        fileStatus: '',
        addProjectUserStatue: false,
        addFileStatus: false,
        payClassify: ['微信', "支付宝", "银行卡", "网银"],
        fileClassifys: "",
        fileClassifysCode: '',
        data: {
            projectName: "",
            clientName: "",
            clientTel: "",
            contract: "",
            startWork: "",
            endTiem: "",
            projectPrice: "",
            payClassify: "",
            projectAddress: "",
            participant: "",
        },
        userId: "",
        code: "",
        plan: false,
        detailList: [],
        showDetail:false,
    },
    prevImg(e) {
        let img = e.currentTarget.dataset.img
        let urls = this.data.detailList.map(item => item.img)
        wx.previewImage({
            current: img,
            urls,
        })
    },
    closeDetail() {
        this.setData({
            showDetail: false,
            detailList: []
        })
    },
    //查看物料组成
    lookMaterials(e) {
        let currentList = e.currentTarget.dataset.child
        this.setData({
            showDetail: true,
            detailList: currentList
        })
    },
    //提交流程进度
    submitPlan(v) {
        console.log(v)
        let _this = this
        ajax({
            url: "/api/projectPaymentProgress/add",
            method: "POST",
            data: {
                amount: v.detail.price,
                files: v.detail.image,
                memo: v.detail.memo,
                projectCode: this.data.code,
                userId: this.data.userId
            }
        }, 'application/json').then(v => {
            if (v.code != 200) {
                wx.showToast({
                    icon: 'none',
                    title: v.msg,
                })
            } else {
                wx.showToast({
                    title: '提交成功！',

                    success() {
                        _this.getProjectDetail()
                        _this.setData({
                            plan: false
                        })
                    }
                })

            }
        })
    },
    closePopup() {
        this.setData({
            plan: false
        })
    },
    openPlan() {
        this.setData({
            plan: !this.data.plan
        })
    },
    //设置物料成本
    setMaterial() {
        wx.navigateTo({
            url: '/pages/setMaterialCost/index?code=' + this.data.code,
        })
    },
    approvalFnc(result) {
        ajax({
            url: "/api/project/auditingProject",
            method: "POST",
            data: {
                projectCode: this.data.code,
                result,
            }
        }).then(v => {
            if (v.code == 200) {
                wx.showToast({
                    title: v.msg,
                    duration: 800
                })
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1,
                    })
                }, 800);
            }
        })
    },
    //审批
    approval() {
        wx.showModal({
            title: "审批",
            content: '是否审批通过当前项目？',
            confirmText: "同意",
            cancelText: "拒绝",
            confirmColor: "#3d4e",
            cancelColor: "red",
            success: res => {
                this.approvalFnc(res.confirm)
            }
        })
    },
    closeaddFile() {
        wx.setNavigationBarTitle({
            title: '项目详情',
        })
        if (this.data.addFileStatus) {
            wx.pageScrollTo({
                duration: 200,
                scrollTop: 50000000
            })
        }
        this.setData({
            addFileStatus: false
        })
    },
    addFile(e) {
        wx.navigateTo({
            url: '/pages/enclosure/index?filecode=' + e.currentTarget.dataset.classifycode + '&ProJectCode=' + this.data.code + '&userId=' + this.data.userId,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            code: options.code
        })
        let userId = wx.getStorageSync('userId')
        if (userId) {
            this.setData({
                userId
            })
        } else {
            wx.showToast({
                title: '登录信息失效，请重新登录',
                success: () => {
                    wx.switchTab({
                        url: '/pages/login/index',
                    })
                }
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getProjectDetail()
    },
    getProjectDetail() {
        ajax({
            url: "/api/project/projectDetail",
            method: 'post',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                userId: this.data.userId,
                projectCode: this.data.code

            }
        }).then(values => {
            let v = values.data
            let fileStatus = v.fileClassifys.length
            let planResult = v.projectPaymentProgress
            this.setData({
                planResult,
                fileStatus,
                editPackBtn: v.editPackBtn,
                auditingBtn: v.auditingBtn,
                packs: v.packs,
                fileClassifys: v.fileClassifys,
                'data.projectName': v.projectName,
                'data.clientName': v.customerName,
                'data.clientTel': v.customerTel,
                'data.contract': v.startDate,
                'data.startWork': v.startDate,
                'data.endTiem': v.endDate,
                'data.projectPrice': v.payAmount,
                'data.payClassify': v.payModel,
                'data.projectAddress': v.projectAddress,
                'data.participant': v.participants,
                'data.payAmount': v.payAmount,
            })
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})